package com.yugao.fintech.antelope.mps.controller.captcha;

import com.yugao.fintech.antelope.base.security.annotation.Anonymous;
import com.yugao.fintech.antelope.base.security.annotation.InnerAuth;
import com.yugao.fintech.antelope.base.model.constants.TenantCons;
import com.yugao.fintech.antelope.mps.api.model.rqrs.CheckNoAuthUserCaptchaReq;
import com.yugao.fintech.antelope.mps.api.model.rqrs.SendCaptchaMsgReq;
import com.yugao.fintech.antelope.mps.service.CaptchaMessageService;
import com.yugao.fintech.antelope.base.tenant.core.utils.TenantUtils;
import com.yugao.fintech.framework.core.response.Resp;
import com.yugao.fintech.framework.security.ratelimit.LimitTypeEnum;
import com.yugao.fintech.framework.security.ratelimit.RateLimit;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 一般情况下当用于指定 登录 | 找回密码 | 注册用户等操作时, 将发送验证码(匿名用户)
 */
@RestController
@RequestMapping("/captchaMsg")
public class NoAuthUserCaptchaMsgController {
    @Autowired
    private CaptchaMessageService captchaMessageService;

    /**
     * 针对登录时候的验证码, 无需登录
     */
    @PostMapping("/send")
    @RateLimit(key = "rate_limit::verify_message", time = 120, count = 4, limitType = LimitTypeEnum.IP,
            msg = "操作过于频繁, 请 ${remainingTime} 后再次尝试")
    @Anonymous
    public Resp<?> sendCaptcha(@Validated @RequestBody SendCaptchaMsgReq req) throws Exception {
        TenantUtils.execute(TenantCons.SYSTEM_TENANT_ID, () ->  captchaMessageService.sendNoAuthUserCaptcha(req));
        return Resp.ok();
    }

    @InnerAuth
    @PostMapping("/check")
    public Resp<?> checkCaptcha(@Validated @RequestBody CheckNoAuthUserCaptchaReq req) {
        TenantUtils.execute(TenantCons.SYSTEM_TENANT_ID, () ->  captchaMessageService.checkNoAuthUserCaptcha(req));
        return Resp.ok();
    }

}
